package com.kaifamiao.www.demo.do0810;



/**

 * 双端链表
 */
  public class DoubleNodeList {

    private Node head;

    private Node last;

    private int length;

    public boolean add(String value) {

        Node node = new Node(last,value);

        if (head == null) {
            head = node;
        } else {
            last.next = node;
        }
        last = node;
        length++;
        return true;
    }

    public String remove(int index) {
        checkIndex(index);
        Node current = null;
        if (index == 0) {
            current = head;
            head = head.next;
            head.prev = null;
            current.next = null;
        } else if (index == length - 1) {
            current = last;
            last = current.prev;
            current.prev = null;
            last.next = null;
        } else {
            //找到被删除的元素
            current = head;
            for (int i = 1; i <= index; i++) {
                current = current.next;
            }
            //  current.prev.next = current.next;
            //  current.next.prev = current.prev;
            //被删除元素的上一个元素
//        Node prev = current.prev;
//        //被删除元素的下一个元素
//        Node next = current.next;
            //上一个元素的next指向下一个元素，下一个元素的prev指向上一个元素
            current.prev.next = current.next;
            current.next.prev = current.prev;
            current.next = null;
            current.prev = null;
        }
        length--;
        return current.value;
    }

    public String set(int index, String value) {
        checkIndex(index);
        Node current = head;
        for (int i = 1; i <= index; i++) {
            current = current.next;
        }
        String old = current.value;
        current.value = value;
        return old;
    }
    public boolean add(int index,String value){
        checkIndex0(index);
        Node current = null;
        Node node = new Node(value);
        if (index == 0){
            node.next = head;
            head = node;
        }else if (index == length){
            current = last;
            last = node;
            current.next = node;
            node.prev = current;
        }
        length++;
        return true;
    }


    public int size() {

        return length;
    }

    private void checkIndex0(int index){
        if (index < 0 || index > length){
            throw new NodeListIndexOutOfBoundsException("索引异常，index 应该是 0 到 "+ length +".但接收到的是"+index);
        }
    }

    private void checkIndex(int index) {
        if (index < 0 || index >= length) {
            throw new NodeListIndexOutOfBoundsException("索引异常,index应该是0到" + (length - 1) + "。但是接收到的是" + index);
        }
    }

    private class NodeListIndexOutOfBoundsException extends IndexOutOfBoundsException {
        public NodeListIndexOutOfBoundsException(String msg) {
            super(msg);
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("DoubleNodeList { ");
        String str = "";
        if (head != null) {
            Node current = head;
            while (current != null) {
                sb.append(current + ",");
                current = current.next;
            }
            str = sb.substring(0, sb.length() - 1);
        } else {
            str = sb.toString();
        }
        str += " }";
        return str;
    }


    private class Node {
        private Node prev;

        private String value;

        private Node next;

        public Node(){}

        public Node(Node prev, String value) {
            this.prev = prev;
            this.value = value;
        }

        public Node(String value) {

            this.value = value;
        }

        public Node(Node prev, String value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }

        @Override
        public String toString() {
            return this.value;
        }

    }


}